package cn.hml.org.controller;

import cn.hml.basic.util.AjaxResult;
import cn.hml.basic.util.PageList;
import cn.hml.org.domain.Systemdictionarydetail;
import cn.hml.org.query.SystemdictionarydetailQuery;
import cn.hml.org.service.ISystemdictionarydetailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
//@Controller
//@ResponseBody
@RequestMapping("/systemdictionarydetail")
@CrossOrigin
@Api(value = "部门接口类",description = "处理部门的所有请求")
public class SystemdictionnarydetailController {

    @Autowired
    private ISystemdictionarydetailService systemdictionarydetailService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    //写一个接口/方法：1.请求方式 2.请求参数 3.请求地址 4.返回数据格式
    //Get http://localhost/systemdictionarydetail/1
    @GetMapping("/{id}")
    @ApiOperation("查询单个对象")
    //@PathVariable("id") Long id - 从路径上去获取变量 id 赋值给 Long id
    public Systemdictionarydetail loadById(@PathVariable("id") Long id){
        return systemdictionarydetailService.loadById(id);
    }

    /**
     * 接口：查询所有
     * @return
     */
    //Get http://localhost/systemdictionarydetail
    @GetMapping
    @ApiOperation("查询所有对象")
    public List<Systemdictionarydetail> loadAll(){
        return systemdictionarydetailService.loadAll();
    }

    /**
     * 接口：分页查询
     * @param systemdictionarydetailQuery
     * @return
     */
    //post http://localhost/systemdictionarydetail 对象
    @PostMapping
    @ApiOperation("分页查询")
    public PageList<Systemdictionarydetail> queryPage(@RequestBody SystemdictionarydetailQuery systemdictionarydetailQuery){
        return systemdictionarydetailService.queryPage(systemdictionarydetailQuery);
    }

    /**
     * 删除接口
     * @param id
     * @return
     */
    //delete http://localhost/systemdictionarydetail/1
    @DeleteMapping("/{id}")
    @ApiOperation("删除单个对象")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            systemdictionarydetailService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 添加和修改接口
     * @param systemdictionarydetail
     * @return
     */
    //put http://localhost/systemdictionarydetail {对象}
    @PutMapping
    @ApiOperation("添加或修改")
    public AjaxResult addOrUpdate(@RequestBody Systemdictionarydetail systemdictionarydetail){
        try {
            if(systemdictionarydetail.getId() == null){
                systemdictionarydetailService.sava(systemdictionarydetail);
            }else{
                systemdictionarydetailService.update(systemdictionarydetail);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }


    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation("批量删除对象")
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            systemdictionarydetailService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

}
